package com.homegoods.classify.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.homegoods.classify.R;

import java.util.List;

/**
 * add shibo.zheng
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> implements View.OnClickListener {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<String> mDatas;
    public int position;

    public RecycleViewAdapter(Context context , List<String> datas, int pos){
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
        position = pos;
    }

    @Override
    public void onClick(View v) {

    }

    //设置分类导航栏点击事件
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.classify_item_single_textview, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
        holder.tv.setText(mDatas.get(i));

        holder.tv.setBackgroundColor(Color.parseColor("#FAFAFA"));
        holder.tv.setTextColor(Color.parseColor("#969696"));
        holder.red.setVisibility(View.GONE);
        //默认字体不加粗
        TextPaint paint = holder.tv.getPaint();
        paint.setFakeBoldText(false);
        if(i == position){
            holder.tv.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.color_a541c9));
            //设置默认字体加粗
            TextPaint paint1 = holder.tv.getPaint();
            paint1.setFakeBoldText(true);
            //设置红边出现
            holder.red.setVisibility(View.VISIBLE);
        }

        if(mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickListener.onItemClick(holder.itemView, i);
                }
            });

//            //longclick
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    mOnItemClickListener.onItemLongClick(holder.itemView,pos);
//                    return false;
//                }
//            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        TextView red;
        ViewPager mainViewPager;

        public MyViewHolder(View arg0){
            super(arg0);

            tv = arg0.findViewById(R.id.id_tv);
            red = arg0.findViewById(R.id.id_red);
            mainViewPager = arg0.findViewById(R.id.child_container_ll);

        }
    }
}
